package com.fhzn.boss.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fhzn.boss.context.RequestContext;
import com.fhzn.boss.entity.Label;
import com.fhzn.boss.mapper.LabelMapper;
import com.fhzn.boss.service.LabelService;
import com.fhzn.boss.web.converter.LabelConverter;
import com.fhzn.boss.web.request.LabelQueryRequest;
import com.fhzn.boss.web.request.LabelsOpRequest;
import com.fhzn.boss.web.vo.LabelVO;
import com.fhzn.commons.toolkit.exception.BuzException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 老顾
 * @apiNote
 * @since 2024/3/25
 */
@Service
@RequiredArgsConstructor
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label> implements LabelService {

    private final LabelMapper labelMapper;


    @Override
    public List<Label> labelSetBasedOnAppcode(String appCode) {
        return labelMapper.queryLabelSetBasedOnAppcodeList(appCode);
    }

    @Override
    public Page<Label> labelsWithPage(LabelQueryRequest request) {
        request.getOffset();
        return labelMapper.queryLabelWithPage(request);
    }

    @Override
    public Integer addLabels(LabelsOpRequest request) {
        Label label = LabelConverter.fromOpLabel(request);
        return labelMapper.insert(label);
    }

    @Override
    public Integer updateLabels(LabelsOpRequest request) {
        Label label = LabelConverter.fromOpLabel(request);
        try {
            return labelMapper.updateById(label);
        } catch (Exception e) {
            throw new BuzException("标签信息重复！");
        }
    }

    @Override
    public void deleteLabels(Integer id) {
        labelMapper.deleteByLabelId(id, RequestContext.getRequestData().getHandler());
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void importLabels(String data) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(data);
        JsonNode dataArray = jsonNode.get("data");
        if (dataArray.isArray()) {
            for (JsonNode labelNode : dataArray) {
                String labelString = labelNode.asText();
                String[] parts = labelString.split(":");
                if (parts.length == 4) {
                    String code = parts[0].trim();
                    String name = parts[1].trim();
                    int parentId = Integer.parseInt(parts[2].trim());
                    String appCode = parts[3].trim();

                    LabelVO labelVO = new LabelVO();
                    labelVO.setCode(code);
                    labelVO.setName(name);
                    labelVO.setParentId(parentId);
                    labelVO.setAppCode(appCode);

                    Label label = LabelConverter.fromLabelVO(labelVO);

                    // 如果不存在，插入新记录
                    labelMapper.insert(label);
                }
            }
        }
    }
}
